package kgame5.k25d.helper {
	import kgame5.k25d.Util25D;
	import kgame5.k25d.core.Camera25D;
	import kgame5.k25d.core.Node25D;
	import kgame5.k25d.core.group.World25D;
	import kgame5.k3d.core.math.Vector3D;
	import kgame5.kutil.KEvent;
	import kgame5.kutil.T;

	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author max
	 * 2010-12-14
	 * 跟交互相关的，能检测内部事件
	 * 摄像机拖拽器
	 * 因为是25D,所以直接拖拽
	 * 
	 * TODO 把world内的各个物体做成mouseEanbel=false,mouseChildre=false能提高性能（如有必要）
	 * TODO 不能超过边界
	 */
	public class MouseInput25D{
		
		
		
		private var camera:Camera25D;
		private var mc:DisplayObject;//拖拽
		private var stage:Stage;
		private var world:World25D;
		public function MouseInput25D(world:World25D){
			
			this.world=world;
			this.mc=world.getDO();
			this.camera = world.camera;
			if(mc.stage){
				onAddToStage(null);
			}else{
				mc.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			}
			
		}
		
		private function onAddToStage(e:Event):void{
			this.stage=mc.stage;
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			stage.addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		private var preX:Number;
		private var preY:Number;
		
		private function onMouseDown(e:MouseEvent):void{
			preX=e.stageX;
			preY=e.stageY;
			
			camera.dragging=true;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		}
		
		private function onMouseUp(e:MouseEvent):void{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			camera.dragging=false;
			
			//更新target位置
			//摄像机屏幕中心的坐标
			var screenX:int=camera.viewRect.x+camera.viewRect.width/2;
			var screenY:int=camera.viewRect.y+camera.viewRect.height/2;
			
			Util25D.screenToWorld(screenX,screenY, camera.target);
			
			//camera.target.x=screenY+screenX/2;
			//camera.target.z=screenY-screenX/2;
		}

		private function onMouseMove(e:MouseEvent):void{
			
			camera.viewRect.x+=-(e.stageX-preX);
			camera.viewRect.y+=-(e.stageY-preY);
			
			preX=e.stageX;
			preY=e.stageY;
		}
		
		private function onMouseClick(e:MouseEvent):void{
			T.p("onMouseClick");
			var screenX:Number=e.stageX+ camera.viewRect.x;
			var screenY:Number=e.stageY+ camera.viewRect.y;
			
			T.p("stage",e.stageX,e.stageY,"viewRect",camera.viewRect);
			T.p("screen",screenX,screenY);
			
			var v3d_p:Vector3D=new Vector3D();
			Util25D.screenToWorld(screenX,screenY, v3d_p);
			
			T.p("world",v3d_p);
			
			var result:Array=[];
			Util25D.queryPoint(v3d_p, world, result);
			for each(var node:Node25D in result){
				node.dispatchEvent(new KEvent(Node25D.CLICK,v3d_p));
			}
			//this.dispatchEvent(new KEvent(CLICK_ITEM,result));
			
//			//--测试用的
//			if(result.length>0){
//				(result[0] as Node25D).spcVO.v3d_p.y+= 64;
//			}
		}
	}//end of class
}
